package com.simon.train.${module}.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simon.train.common.context.LoginMemberContext;
import com.simon.train.common.util.SnowUtil;
import com.simon.train.${module}.domain.${Domain};
import com.simon.train.${module}.domain.${Domain}Example;
import com.simon.train.${module}.dto.${Domain}QueryDto;
import com.simon.train.${module}.dto.${Domain}SaveDto;
import com.simon.train.${module}.mapper.${Domain}Mapper;
import com.simon.train.common.vo.PageVO;
import com.simon.train.${module}.vo.${Domain}QueryVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ${Domain}Service {

    private static final Logger LOG = LoggerFactory.getLogger(${Domain}Service.class);

    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    public void Save${Domain}(${Domain}SaveDto ${domain}SaveDto){
        ${Domain} ${domain} = BeanUtil.copyProperties(${domain}SaveDto, ${Domain}.class);
        if (ObjectUtil.isNull(${domain}.getId())){
            //对于首次保存 也就是新增 需要对字段单独做处理
            ${domain}.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
<#--            ${domain}.setMemberId(LoginMemberContext.getId());-->
            ${domain}.setCreateTime(time);
            ${domain}.setUpdateTime(time);
            ${domain}Mapper.insert(${domain});
        } else {
            ${domain}.setUpdateTime(DateTime.now());
            ${domain}Mapper.updateByPrimaryKey(${domain});
        }

    }

    public PageVO<${Domain}QueryVO> Query${Domain}List(${Domain}QueryDto ${domain}QueryDto){
        ${Domain}Example ${domain}Example = new ${Domain}Example();
        ${domain}Example.setOrderByClause("id desc");
        ${Domain}Example.Criteria criteria = ${domain}Example.createCriteria();
<#--        if (ObjectUtil.isNotNull(${domain}QueryDto.getMemberId())){-->
<#--            criteria.andMemberIdEqualTo(${domain}QueryDto.getMemberId());-->
<#--        }-->
        LOG.info("当前请求的页码：{}", ${domain}QueryDto.getPage());
        LOG.info("每页条数：{}", ${domain}QueryDto.getPageSize());

        PageHelper.startPage(${domain}QueryDto.getPage(),${domain}QueryDto.getPageSize());
        List<${Domain}> ${domain}List = ${domain}Mapper.selectByExample(${domain}Example);

        PageInfo<${Domain}> pageInfo = new PageInfo<>(${domain}List);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<${Domain}QueryVO> lists = BeanUtil.copyToList(${domain}List, ${Domain}QueryVO.class);
        PageVO<${Domain}QueryVO> pageVO = new PageVO<>();
        pageVO.setList(lists);
        pageVO.setTotal(pageInfo.getTotal());

        return pageVO;
    }

    public void delete${Domain}(Long id) {
        ${domain}Mapper.deleteByPrimaryKey(id);
    }
}
